package com.example.demo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    String jsonStr = "[\n" +
            "    {\"name\": \"jadyli\", \"gender\": \"male\"},\n" +
            "    {\"name\": \"sanqi\", \"gender\": \"male\"}\n" +
            "]";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         findViewById(R.id.btn_1).setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 try {
                     JSONArray jsonArray = new JSONArray(jsonStr);
                     for (int i =0;i<jsonArray.length();i++){
                         JSONObject jsonObject = jsonArray.getJSONObject(i);
                         String gender = jsonObject.getString("gender");
                         String name = jsonObject.getString("name");

                         Log.e("MainActivity","name="+name+"gender="+gender);
                     }
                 } catch (JSONException e) {
                     e.printStackTrace();
                 }
             }
         });
        final Student s1 = new Student("张三",20,"1713150");
        final Student s2 = new Student("张四",21,"1713151");
        final Student s3 = new Student("张五",22,"1713152");
        final ArrayList<Student> studentArrayList = new ArrayList<Student>();


         findViewById(R.id.btn_2).setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 JSONObject jsonObject = new JSONObject();

                 try {
                     jsonObject.put("name",s1.getName());
                     jsonObject.put("name",s1.getAge());
                     jsonObject.put("name",s1.getStuNo());

                     Log.e("MainActivity","jsonObject="+jsonObject);
                 } catch (JSONException e) {
                     e.printStackTrace();
                 }

             }
         });
         findViewById(R.id.btn_3).setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 JSONArray jsonArray = new JSONArray();
                 for (Student student :studentArrayList){
                     JSONObject jsonObject = new JSONObject();
                     try {
                         jsonObject.put("name",s1.getName());
                         jsonObject.put("name",s1.getAge());
                         jsonObject.put("name",s1.getStuNo());
                     } catch (JSONException e) {
                         e.printStackTrace();
                     }
                     jsonArray.put(jsonObject);
                 }
                 JSONObject jsonObject = new JSONObject();
                 try {
                     jsonObject.put("studList",jsonArray);
                 } catch (JSONException e) {
                     e.printStackTrace();
                 }

             }
         });



         findViewById(R.id.btn_4).setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 try {
                     InputStream inputStream = getResources().getAssets().open("province.txt");

                     BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

                     StringBuffer stringBuffer = new StringBuffer();

                     String line = bufferedReader.readLine();

                     while (line!=null){
                         stringBuffer.append(line);
                         line = bufferedReader.readLine();

                     }
                     JSONArray provinceJsonArray = new JSONArray(stringBuffer.toString());

                     for (int i =0;i<provinceJsonArray.length();i++){
                         JSONObject provinceJsonObject = provinceJsonArray.getJSONObject(i);
                         String provinceName = provinceJsonObject.getString("name");


                         /*Log.e("MainActivity","provinceName="+provinceName);*/

                         if (provinceName.equals("广东省")){
                             JSONArray cityJsonArray = provinceJsonObject.getJSONArray("city");
                             for (int j=0;j<cityJsonArray.length();j++){
                                 JSONObject cityObject = cityJsonArray.getJSONObject(j);
                                 String cityName = cityObject.getString("name");



                                  if (cityName.equals("广州市")){
                                      JSONArray areajsonArray = cityObject.getJSONArray("area");
                                      for (int k=0;k<areajsonArray.length();k++){
                                          String areaName = areajsonArray.getString(k);
                                          Log.e("MainActivity","area="+areaName);
                                      }
                                  }
                             }
                         }
                     }

                     /*Log.e("MainActivity","stringJson="+stringBuffer.toString());*/
                 } catch (IOException e) {
                     e.printStackTrace();
                 } catch (JSONException e) {
                     e.printStackTrace();
                 }

             }
         });
    }
}
